package com.edward.service;


import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.edward.PagerModel;
import com.edward.model.ShallCei;


public class ShallCeiManager extends HibernateDaoSupport {

	public void addShallCei(ShallCei shallCei) {
		
		getHibernateTemplate().save(shallCei);
	}
	
	public void updateShallCei(ShallCei shallCei) {
		
		getHibernateTemplate().update(shallCei);
	}
	
	public void deleteShallCei(int id){
		
		getHibernateTemplate().delete(getShallCei(id));
	}
	
	public ShallCei getShallCei(int id){
		ShallCei d =(ShallCei)getHibernateTemplate().get(ShallCei.class, id);
		return d;
	}
	

	@SuppressWarnings("unchecked")
	public List<ShallCei> listShallCei() {
		return getHibernateTemplate().find("from ShallCei");
	}

	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,String type) {
		List <ShallCei>list = getHibernateTemplate().find("from ShallCei s where s.type = '" + type +"' order by s.name" );
		int size = list.size();
		String name = "";
		int id = 999999;
		int t = 0;
		List <ShallCei>l = new ArrayList<ShallCei>();
		 ShallCei s = new ShallCei();
		 s.setName("合计");
		for(int i=0; i<size;i++){
			  s.setMoney(s.getMoney() + list.get(i).getMoney());
			  s.setRepay(s.getRepay() + list.get(i).getRepay());
			  s.setBalance(s.getBalance() + list.get(i).getBalance());
			if(name.equals(list.get(i).getName())){
				if(t == 0){
				 
				  ShallCei sc = new ShallCei();
				  sc.setId(id);
				  id++;
				  sc.setName(name);
				  sc.setMoney(list.get(i).getMoney() + list.get(i-1).getMoney());
				  sc.setRepay(list.get(i).getRepay() + list.get(i-1).getRepay());
				  sc.setBalance(list.get(i).getBalance() + list.get(i-1).getBalance());
				  l.add(l.size()-1, sc);
				  l.add(list.get(i));
				  t++;
				}else{
					ShallCei sc = l.get(l.size() - t - 2);
					 sc.setMoney(sc.getMoney() + list.get(i).getMoney());
					 sc.setRepay(sc.getRepay() + list.get(i).getRepay());
					 sc.setBalance(sc.getBalance() + list.get(i).getBalance());
					 l.add(list.get(i));
					 t++;
				}
			}else{
			 name = list.get(i).getName();
			 l.add(list.get(i));
			 t = 0;
			}
			
		}
		l.add(s);
		int total = l.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShallCei>datas = l.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	
	@SuppressWarnings("unchecked")	
	public PagerModel pagerList(int start,int limit,ShallCei shallCei) {
		
		List <ShallCei>list;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String sql = "from ShallCei s where s.type = '" + shallCei.getType() + "'";
		if(!shallCei.getName().equals("")){
			sql += "and s.name = '" + shallCei.getName() + "'";
		}
		
		if(shallCei.getRepayDate() != null){
			String d = sdf.format(shallCei.getRepayDate());
			sql += "and  s.repayDate >= '" + d + "'";
		}
		
		if(shallCei.getDate() != null && shallCei.getEndDate() == null ){
			String d = sdf.format(shallCei.getDate());
			
			sql += "and  s.date >= '" + d + "'";
		}
		if(shallCei.getEndDate() != null && shallCei.getDate() == null ){
			String d = sdf.format(shallCei.getEndDate());
		
			sql += "and  s.date <= '" + d + "'";
		}
		if(shallCei.getEndDate() != null && shallCei.getDate() != null ){
			String d1 = sdf.format(shallCei.getDate());
			String d2 = sdf.format(shallCei.getEndDate());
			
			sql += "and s.date between '" + d1 + "' and '" +d2  +"'";
		}
		list = getHibernateTemplate().find(sql);
		int size = list.size();
		String name = "";
		int id = 999999;
		int t = 0;
		List <ShallCei>l = new ArrayList<ShallCei>();
		 ShallCei s = new ShallCei();
		 s.setName("合计");
		for(int i=0; i<size;i++){
			  s.setMoney(s.getMoney() + list.get(i).getMoney());
			  s.setRepay(s.getRepay() + list.get(i).getRepay());
			  s.setBalance(s.getBalance() + list.get(i).getBalance());
			if(name.equals(list.get(i).getName())){
				if(t == 0){
				 
				  ShallCei sc = new ShallCei();
				  sc.setId(id);
				  id++;
				  sc.setName(name);
				  sc.setMoney(list.get(i).getMoney() + list.get(i-1).getMoney());
				  sc.setRepay(list.get(i).getRepay() + list.get(i-1).getRepay());
				  sc.setBalance(list.get(i).getBalance() + list.get(i-1).getBalance());
				  l.add(l.size()-1, sc);
				  l.add(list.get(i));
				  t++;
				}else{
					ShallCei sc = l.get(l.size() - t - 2);
					 sc.setMoney(sc.getMoney() + list.get(i).getMoney());
					 sc.setRepay(sc.getRepay() + list.get(i).getRepay());
					 sc.setBalance(sc.getBalance() + list.get(i).getBalance());
					 l.add(list.get(i));
					 t++;
				}
			}else{
			 name = list.get(i).getName();
			 l.add(list.get(i));
			 t = 0;
			}
			
		}
		l.add(s);
		int total = l.size();
		int toIndex = start + limit;
		if(toIndex>total){
			toIndex =  total;
		}
		List <ShallCei>datas = l.subList(start, toIndex);
		PagerModel pm = new PagerModel();
		pm.setTotal(total);
		pm.setDatas(datas);
		return pm;
		
	}
	


}



